package com.teacold.customdemo.chapter_one;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.teacold.utils.Logger;

public class RectDemo extends View {


    public RectDemo(Context context) {
        super(context);
    }

    public RectDemo(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RectDemo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        RectUnionSpot(paint, canvas);


    }

    private void RectUnionSpot(Paint paint, Canvas canvas) {
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.YELLOW);
        Rect rect1 = new Rect(10, 10, 20, 20);
        rect1.union(100, 100);
        Logger.e("Rect1:", rect1.toShortString());
        canvas.drawRect(rect1, paint);
        paint.setColor(Color.RED);
        Rect rect2 = new Rect();
        rect2.union(100, 100);
        Logger.e("Rect12:", rect1.toShortString());
        canvas.drawRect(rect2, paint);
    }


    private void RectUnion(Paint paint, Canvas canvas) {//合并两个Rect
        paint.setStyle(Paint.Style.STROKE);
        Rect rect1 = new Rect(10, 10, 20, 20);
        Rect rect2 = new Rect(100, 100, 110, 110);

        paint.setColor(Color.GREEN);
        canvas.drawRect(rect1, paint);
        paint.setColor(Color.RED);
        canvas.drawRect(rect2, paint);
        //画出合并之后的结果 rect_1
        paint.setColor(Color.YELLOW);
        rect1.union(rect2);
        canvas.drawRect(rect1, paint);
    }

    private void RectIntersects(Paint paint, Canvas canvas) {//是否相交
        paint.setStyle(Paint.Style.FILL);
        Rect rect1 = new Rect(10, 10, 200, 200);
        Rect rect2 = new Rect(190, 10, 250, 200);
        Rect rect3 = new Rect(10, 210, 200, 300);

        paint.setColor(Color.WHITE);
        canvas.drawRect(rect1, paint);
        paint.setColor(Color.RED);
        canvas.drawRect(rect2, paint);
        paint.setColor(Color.BLUE);
        canvas.drawRect(rect3, paint);
        Logger.e("RectIntersects:", "Rect1&Rect2:" + Rect.intersects(rect1, rect2) + "Rect1&Rect3" + Rect.intersects(rect1, rect3));
    }

}
